program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  m, n, i, exchange, j: integer;
  arr: array of integer;

procedure Heapify (num: integer);
var a, b, ex: integer;
begin
  while (1=1) do
  begin
    a:=2000000;
    b:=2000000;
    if (2*num+1<N) then
      a:=arr[2*num+1];
    if (2*num+2<N) then
      b:=arr[2*num+2];
    if (a<=b) and (a<arr[num]) then
    begin
      ex:=arr[num];
      arr[num]:=arr[2*num+1];
      arr[2*num+1]:=ex;
      num:=2*num+1;
    end else if (b<=a) and (b<arr[num]) then
    begin
      ex:=arr[num];
      arr[num]:=arr[2*num+2];
      arr[2*num+2]:=ex;
      num:=2*num+2;
    end else
      break;
  end;
end;



begin
readln(N);
setlength(arr, N);
for i:=0 to N-1 do
begin
  arr[i]:=random(50);
  write(arr[i], ' ');
end;
writeln;
for j:=0 to n-1 do
begin
  for i:= N-1 downto 0 do
    Heapify(i);
  M:=N;
  for i:=0 to M-1 do
  begin
    exchange:=arr[0];
    arr[0]:=arr[N-1];
    arr[N-1]:=exchange;
    dec(N);
    Heapify(0);
  end;
  N:=M;
end;
for i:=0 to N-1 do
begin
  write(arr[i], ' ');
end;
readln;
end.

